Elements or Attributes?

The question arises: “When should I use an element and when should I use an attribute?”  This is going to be a topic that will be debated for a long time.  There are really no clear guidelines on this one.  Many have tried to throw some basic ones together, but for the most part, they really don’t solve the issue.  Here are some guidelines that have been used by many.  The first three are really the most pragmatic and useful, but the others can shed some light when looking at XML documents authored by others.

Use whatever is more readable.

When in doubt, use an element.  Elements typically make more sense to others (are more readable) when you give them an XML document.

Use elements to represent objects, attributes to represent properties of that object.

Use elements to represent physical aspects.  Use attributes to represent intangible, abstract aspects.

If you are using an XML editor, it may be easier to use elements since some editors have trouble with attributes.

The real answer to the question is: “it depends”.  It depends on you, the author.  It is up to you to create content models that accurately represent the structure of the data that you are trying to capture.  It is also up to you to create entities (XML documents) that are readable and maintainable.

There are features in XML that require you to use one over the other.  This has not been a discussion about that.  What we are discussing now is a preference issue.

Tip:	Since there is no “magic bullet” to solving the problem, the best recommendation is to use good common sense and create entities that make sense.